package com.zhcx.zhcxlibrary.utils;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.animation.LinearInterpolator;

/**
 * ProjectName：Wisdomtransit
 * PackageName：com.zhcx.wisdomtransit.utils
 * description:
 * Date：2016/11/3 14:19
 * Author：Steven
 * ClassName:RotateAnimation
 **/
public class RotateAnimation {
    private ObjectAnimator imageViewObjectAnimator;

    public RotateAnimation with(@NonNull View view){
        this.view = view;
        return this;
    }

    public static RotateAnimation create(){
        return new RotateAnimation();
    }

    public void start(){

        if (view == null) throw new NullPointerException("View cant be null!");

        imageViewObjectAnimator = ObjectAnimator.ofFloat(view, "rotation", 0f, 360f);
        imageViewObjectAnimator.setDuration(duration); // miliseconds
        imageViewObjectAnimator.setRepeatMode(repeatMode);
        imageViewObjectAnimator.setRepeatCount(repeatCount);
        imageViewObjectAnimator.setInterpolator(new LinearInterpolator());
        imageViewObjectAnimator.start();
    }

    public void stop(){
        if (null != imageViewObjectAnimator && imageViewObjectAnimator.isRunning()){
            imageViewObjectAnimator.pause();
            imageViewObjectAnimator.cancel();
        }
    }

    public boolean isShow(){
        if (null != imageViewObjectAnimator){
            if (imageViewObjectAnimator.isStarted() && imageViewObjectAnimator.isRunning()){
                return true;
            }else {
                return false;
            }
        }else {
            return false;
        }
    }

    private int duration = 18000;
    private int repeatMode = ValueAnimator.RESTART;
    private int repeatCount = INFINITE;
    private View view;

    public static final int RESTART = 1;
    public static final int REVERSE = 2;
    public static final int INFINITE = -1;

    public RotateAnimation setDuration(int duration){
        this.duration = duration;
        return this;
    }

    public RotateAnimation setRepeatMode(int repeatMode){
        this.repeatMode = repeatMode;
        return this;
    }

    public RotateAnimation setRepeatCount(int repeatCount){
        this.repeatCount = repeatCount;
        return this;
    }
}
